package com.cj;

import com.cj.handler.DemoProxyHandler;
import com.cj.interceptor.CglibProxyInterceptor;
import com.cj.interceptor.CglibProxyInterceptor2;
import com.cj.service.CglibProxy;
import com.cj.service.DemoProxyJDK;
import com.cj.service.impl.CglibTarget;
import com.cj.service.impl.DemoProxyJDKImpl;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InterfaceMaker;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author caojie
 * @version 1.0
 * @Description
 * @date 2023/6/30 15:08
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        // 获取代理对象
//        DemoProxyJDK demoProxyJDK = (DemoProxyJDK) Proxy.newProxyInstance(DemoProxyJDKImpl.class.getClassLoader(),
//                new Class[]{DemoProxyJDK.class},
//                new DemoProxyHandler(new DemoProxyJDKImpl())
//        );
//        demoProxyJDK.method1("cj");
//        SpringApplication.run(Application.class);

//        CglibTarget cglibTarget = (CglibTarget) Enhancer.create(CglibTarget.class, new CglibProxyInterceptor());
//        cglibTarget.test1();

        // 先用接口生成器 对我们要实现的接口做一个处理
        InterfaceMaker interfaceMaker=new InterfaceMaker();
        interfaceMaker.add(CglibProxy.class);
        Class aClass = interfaceMaker.create();

        CglibTarget cglibTarget = (CglibTarget) Enhancer.create(CglibTarget.class, new Class[]{aClass}, new CglibProxyInterceptor2());
        cglibTarget.test2();

        Method test1 = cglibTarget.getClass().getMethod("test1");
        test1.invoke(cglibTarget);
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.incrementAndGet();
    }
}
